package _interview100;

import _object.TreeNode;

/**
 * 236. 二叉树的最近公共祖先
 */
public class No236 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || root == p || root == q) return root;
        TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
        TreeNode rightNode = lowestCommonAncestor(root.right, p, q);
        if (leftNode != null && rightNode != null) return root;
        else return leftNode != null ? leftNode : rightNode;
    }
}
